<?php
/**
 * 处理警员的添加、修改、删除，根据传递过来的act判断相应的操作
 */
session_start();
if (!isset ($_SESSION["id"])) {
	session_destroy();
	echo "<script language='javascript'>top.location.href = 'Login.php';</script>";
	exit;
}
if(isset($_POST['act'])||isset($_GET['act'])){
	include_once 'public/DB_Oracle.php';
	include_once 'public/Json.php';
	$DBsql = new DB_Oracle ();
	$act=(!empty($_POST['act']))?$_POST['act']:$_GET['act'];
	
	if ($act=='chuli') {//查询某警员相关信息，用于更新警员信息
		$sn=$_POST['sn'];
		$sn=rtrim($sn,',');
		$sql="update gps_alarm_info g set g.alarm_is_handel=1 where g.pos_id in ($sn)";
		//exit($sql);
		$row=$DBsql->query($sql);
		echo 'y';
	}else if ($act=='select') {//查询某警员相关信息，用于更新警员信息
		$sn=$_POST['sn'];
		$sql="select dp.personnel_index,
			       dp.personnel_name,
			       dp.personnel_gender,
			       dp.personnel_sn,
			       dd.department_name,
       			   dd.department_index,
       			   dp.DEVICE_ID
			  from dep_personnel dp, dep_department dd
			 where dp.department_index = dd.department_index
			   and dp.personnel_index = ".$sn."
			 order by dp.personnel_index";
		$row=$DBsql->fetch_one_array($sql);
		echo JSON($row);
	}else if($act=='up'){//更新警员信息
		$number=$_POST['userNumUp'];//警号
		$name=$_POST['userNameUp'];//姓名
		$word=$_POST['workUp'];//派出所
		$sex=$_POST['genderUp']; //性别
		$id=$_POST['hiddUp'];//用于修改警员的id
		$userNo=$_POST['userNoUp']; //手持设备编号
		$sql="update dep_personnel dp
			    set dp.personnel_name   = '$name',
			        dp.personnel_gender = '$sex',
			        dp.personnel_sn     = '$number',
			        dp.department_index = '$word',
			        dp.DEVICE_ID = '$userNo'
			  where dp.personnel_index = '$id'";
		if($DBsql->query($sql)){
			echo '{
				"info":"数据更新成功！",
				"status":"y"
			}';
		}else{
			echo '{
				"info":"数据更新失败！",
				"status":"n"
			}';
		}
	}else if($act=='valid'){//新增警员，先验证警号是否可用
		$number=$_POST['param'];//警号
		$sql="select dp.personnel_index,
			       dp.personnel_name,
			       dp.personnel_gender,
			       dp.personnel_sn,
			       dd.department_name,
       			   dd.department_index
			  from dep_personnel dp, dep_department dd
			 where dp.department_index = dd.department_index
			   and dp.personnel_sn='$number'
			 order by dp.personnel_index";
		$row=$DBsql->fetch_one_array($sql);
		if(isset($row['PERSONNEL_INDEX'])){
			echo '警号已经存在，请更换！';
		}else{
			echo 'y';
		}
	}else if($act=='add'){//增加新警员
		$number=$_POST['userNum'];//警号
		$name=$_POST['userName'];//姓名
		$word=$_POST['work'];//派出所
		$sex=$_POST['gender']; //性别
		$userNo=$_POST['userNo']; //手持设备编号
		$sql="insert into dep_personnel
				  (personnel_index,
				   personnel_name,
				   personnel_gender,
				   department_index,
				   personnel_sn,
				   device_id)
				values
				  (dep_personnel_seq.nextval, '$name', $sex, $word, '$number', '$userNo')";
	if($DBsql->query($sql)){
			echo '{
				"info":"数据更新成功！",
				"status":"y"
			}';
		}else{
			echo '{
				"info":"数据更新失败！",
				"status":"n"
			}';
		}
	}else if($act=='del'){
		if(substr($act,0,3)=='del'){
		$sn=$_POST['sn'];
		$sql="delete from "."gps_alarm_info"." dp where dp.pos_id in ($sn)";
		$DBsql->query($sql);
		echo 'y';
		}
	}
}
?>